package com.dream.cgomall.service;

import lombok.Data;

@Data
public class ServiceResult<T> {
    private boolean success;
    private String message;
    private T result;


    public boolean isSuccess() {
        return success;
    }

    public void setSuccess(boolean success) {
        this.success = success;
    }

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    public T getResult() {
        return result;
    }

    public void setResult(T result) {
        this.result = result;
    }

    public enum Message{
        NOT_FOUND("Not Found Resource"),
        NOT_LOGIN("User Not Login!"),
        HASPHONE("Phone has exist");

        private final String value;

        Message(String value) {
            this.value = value;
        }

        public String getValue() {
            return value;
        }
    }

    public ServiceResult(boolean success) {
        this.success = success;
    }

    public ServiceResult(boolean success, String message) {
        this.success = success;
        this.message = message;
    }

    public ServiceResult(boolean success, T result) {
        this.success = success;
        this.result = result;
    }

    public ServiceResult(boolean success, String message, T result) {
        this.success = success;
        this.message = message;
        this.result = result;
    }
    //成功
    public static <T> ServiceResult<T> success(){
        return new ServiceResult<>(true);
    }
    //找不到数据
    public static <T> ServiceResult<T> notFound(){
        return new ServiceResult<>(false,Message.NOT_FOUND.getValue());
    }
    //失败并返回信息
    public static <T> ServiceResult<T> badResult(String message){
        return new ServiceResult<>(false,message);
    }
    //检验不到手机
    public static <T> ServiceResult<T> HaveNotPhone(){
        return new ServiceResult<>(false,Message.HASPHONE.getValue());
    }
    //返回数据集
    public static <T> ServiceResult<T> of(T result){
        ServiceResult<T> serviceResult=new ServiceResult<>(true);
        serviceResult.setResult(result);
        return serviceResult;
    }
}
